/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.siscoh.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.siscoh.model.bean.AbstractBean;
import org.siscoh.model.bean.CategoriaBean;
import org.siscoh.model.bean.EspecificacaoBean;

/**
 *
 * @author Israel
 */
public class EspecificacaoDao extends AbstractDao {
    
    private boolean lazy;
    
    public EspecificacaoDao()
    {
        lazy = true;
    }
    
    public EspecificacaoDao(boolean isLazy)
    {
        this.lazy = isLazy;
    }

    @Override
    public void update(AbstractBean bean) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insert(AbstractBean bean) {
        EspecificacaoBean espec = (EspecificacaoBean) bean;
        
        long id = Conexao.getInstance().insert("especificacao",
                new String[]{"chave", "categoria_id"},
                new String[]{espec.getChave(), espec.getCategoriaId()+""});
        
        if(id != -1)
        {
            bean.setId(id);
        }
    }
    
    public ArrayList<EspecificacaoBean> selectByCategoria(long catId)
    {
        ResultSet rs = Conexao.getInstance().select(null, "especificacao", "categoria_id = '"+catId+"'");
        
        return parseRsToObjects(rs);
    }

    @Override
    public void delete(AbstractBean bean) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<EspecificacaoBean> fetchAllArrayList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AbstractBean[] fetchAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AbstractBean selectById(long id) {
        ResultSet rs = Conexao.getInstance().select(null, "especificacao", "id = '"+id+"'");
        
        return parseRsToObject(rs);
    }

    @Override
    protected AbstractBean parseRsToObject(ResultSet rs) {
        EspecificacaoBean espec = null;
        try {
            if(rs.next())
            {
                espec = new EspecificacaoBean();
                
                espec.setChave(rs.getString("chave"));
                espec.setId(rs.getLong("id"));
                
                espec.setCategoria((CategoriaBean) new CategoriaDao().selectById(rs.getLong("categoria_id")));
            }
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }
        finally
        {
            try {
                rs.close();
            } catch (SQLException ex) {
                System.err.append(ex.getMessage());
            }
        }
        
        return espec;
    }

    @Override
    protected ArrayList<EspecificacaoBean> parseRsToObjects(ResultSet rs) {
        try {
            ArrayList<EspecificacaoBean> especList = new ArrayList<EspecificacaoBean>();
            
            while(rs.next())
            {
                EspecificacaoBean eb = new EspecificacaoBean();
                eb.setChave(rs.getString("chave"));
                
                long catId = rs.getLong("categoria_id");
                
                if(!lazy)
                {
                    CategoriaBean categBean = (CategoriaBean) new CategoriaDao().selectById(catId);
                
                    eb.setCategoria(categBean);
                }
                eb.setId(rs.getLong("id"));
                
                especList.add(eb);
            }
            
            return especList;
        } catch (SQLException ex) {
            System.err.append(ex.getMessage());
        }
        finally
        {
            try {
                rs.close();
            } catch (SQLException ex) {
                System.err.append(ex.getMessage());
            }
        }
        
        return null;
    }
}